package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.brand.MarketBrand;
import com.barbeque.mall.bean.brand.MarketBrandBO;
import com.barbeque.mall.bean.brand.MarketBrandExample;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 *
 * @author Tezel
 * @date 2022/07/16
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class BrandServiceImpl implements BrandService {

    @Autowired
    MarketBrandMapper brandMapper;


    /**
     * 显示商品信息和查询
     * @author Tezel
     * @date 2022/07/17
     */
    @Override
    public CommonData<MarketBrand> query(Integer id,String name ,BasePageInfo info) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause(info.getSort()+ " " + info.getOrder());

        if(id != null && !"".equals(id)){
            criteria.andIdEqualTo(id);
        }

        if(name != null && !"".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }

        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);


        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);

        return CommonData.data(pageInfo);
    }
    /**
     * 创建品牌商
     * @author Tezel
     * @date 2022/07/17
     */

    //TODO sort_order什么意思
    @Override
    public MarketBrand create(MarketBrandBO marketBrandBO) {

        Date date = new Date(System.currentTimeMillis());
        MarketBrand marketBrand = new MarketBrand();
        BeanUtils.copyProperties(marketBrandBO, marketBrand);
        marketBrand.setUpdateTime(date);
        marketBrand.setAddTime(date);
        marketBrand.setDeleted(false);
        brandMapper.insertSelective(marketBrand);

        Integer id = marketBrand.getId();

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        MarketBrand marketBrandVO = brandMapper.selectByPrimaryKey(id);

        return marketBrandVO;
    }

    /**
     * 编辑更新商品信息
     * @author Tezel
     * @date 2022/07/17
     */
    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(marketBrand.getId());

        Date date = new Date(System.currentTimeMillis());

        marketBrand.setUpdateTime(date);

        brandMapper.updateByExampleSelective(marketBrand,marketBrandExample);

        MarketBrand brand = brandMapper.selectByPrimaryKey(marketBrand.getId());

        return brand;
    }

    /**
     * 删除商品信息
     * @author Tezel
     * @date 2022/07/17
     */
    @Override
    public void delete(MarketBrand marketBrand) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(marketBrand.getId());
        brandMapper.deleteByExample(marketBrandExample);


    }
}
